import entity.BaseModel;
import entity.FillModel;
import entity.TableModel;
import org.beetl.core.Template;
import util.DBUtil;
import util.StringUtil;

import java.io.*;
import java.util.List;
import java.util.Map;

public class BeetlEngine extends DBUtil {

    public static void main(String[] args) throws Exception {
        List<TableModel> modules = bm.getModules();
        Map<String, String> rules = bm.getRules();
        if (bm.isAll())
//        所有表全量生成代码
            generate(rules, modules.get(0), getTables(null));
        else {
//            指定表/模块/生成文件路径
            for (TableModel tm : modules) {
//                指定单模块是否生成代码
                if (tm.isGenerate()) generate(rules, tm, getTables(tm.getTables()));
            }
        }
    }

    private static void generate(Map<String, String> rules, TableModel tm, List<Map<String, String>> tableList) throws IOException {
//        是否生成到测试目录中
        String path = bm.isAll() || bm.isPlay() ? bm.getBasePath() : String.format("%s/%s/", bm.getBaseProject(), bm.getProject());
        File file = new File(path);
        if (!file.exists()) file.mkdirs();
        for (Map<String, String> tMap : tableList) {
            FillModel ml = getFillModel(bm, tMap.get("tableName"));
            ml.setTitle(tMap.get("title"));
            ml.setAddColumn(bm.isAddColumn());
            ml.setSubPackage(tm.getSubPackage());
            String modelName = bm.isAll() ? StringUtil.getDefault(bm.getModelName(), "") : tm.getModelName();
            if (!StringUtil.isEmpty(modelName)) ml.setModelName("." + modelName);
            Map<String, Object> o = ml.asMap();
            for (Map.Entry<String, String> e : rules.entrySet()) {
                String p;
                if (!bm.isPlay()) {
                    if (StringUtil.in("entity,service,controller,tkmapper,mapper", e.getKey()))
                        p = String.format("%s%s/%s", tm.getJavaPath(), modelName, e.getKey().replace("tk", ""));
                    else if (StringUtil.in("xml,tkxml", e.getKey())) p = tm.getXmlPath();
                    else p = String.format("%s%s/", tm.getJsPath(), modelName);
                } else {
                    p = modelName + "/" + e.getKey();
                }

                p = String.format("%s%s/", path, p);
                File f = new File(p);
                if (!f.exists()) f.mkdirs();
                FileOutputStream out = null;
                try {
                    String filePath = p + ml.getEntityName() + e.getValue();
                    System.out.println("生成文件 >>> " + filePath);
                    out = new FileOutputStream(filePath);
                    Template tem = gt.getTemplate(e.getKey() + ".properties");
                    tem.binding(o);
                    String template = ascii2Native(tem.render());
                    out.write(template.getBytes("UTF-8"));
                    out.flush();
                } catch (Exception ex) {
                    ex.printStackTrace();
                } finally {
                    if (out != null) out.close();
                }
            }
        }
    }

    private static FillModel getFillModel(BaseModel bm, String t) {
        FillModel ml = new FillModel();
        ml.setColumns(getDBSchema(t));
        ml.setTable(t);
        ml.setAddColumn(bm.isAddColumn());
        String tb = lineToCamelCdf(t);
        ml.setEntityName(StringUtil.toUpperFirstChar(tb));
        ml.setPrefixEntityName(tb);
        ml.setUrlPrefix(tb);
        ml.setPackageName(bm.getBasePackage());
        return ml;
    }
}
